package icu.ac.kr.phonebook.view;

import icu.ac.kr.phonebook.activity.EditDayDiary;
import icu.ac.kr.phonebook.provider.ConstantProvider;
import icu.ac.kr.phonebook.provider.ReservedWork;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
 
public class ScheduleDetail extends LinearLayout {

	final static String TAG = "ScheduleDetail";
	static final int PRIVATE = 0;
	static final int PUBLIC = 1;
	static final int COUPLE = 2;
	static final int RESERVED = -1;
	static final int VIEWDAYDIARY = 0;
	static int ActivityNumber = VIEWDAYDIARY;
	static int SEARCHSCHEDULE = 1;
	Context context;
	
	TextButton textButton;
	public ScheduleDetail(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub

		this.context = context;
		setOrientation(LinearLayout.VERTICAL);

		
		Log.d(TAG, "construct");
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		textButton.measure(200, 100);
		setMeasuredDimension(500, 1024);
		Log.d(TAG, "onMeasure");
	}
 

	@Override
	public void setOrientation(int orientation) {
		// TODO Auto-generated method stub
		super.setOrientation(orientation);
		Log.d(TAG, "setOrientation");
	}

	

	
	
	
	
	public void bindData(HashMap<Integer, ReservedWork> reservedMap,
			HashMap<Integer, ReservedWork> scheduledMap, final String[] dateArray) {
		// TODO Auto-generated method stub
		
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

		ReservedWork entity = null;
		
		Set<Integer> iKey = reservedMap.keySet();
		Iterator<Integer> it =iKey.iterator();
		while(it.hasNext())
		{
			Log.d(TAG, "Key :" +it.next());
			
		}
		
		for (int i = 1 ; i <= 24; i ++)
		{
			textButton = new TextButton(getContext(), null);
			textButton.setId(i);
			
			entity = (scheduledMap.containsKey(i))?scheduledMap.get(i):reservedMap.get(i);
			if (reservedMap.containsKey(i) || scheduledMap.containsKey(i) )
			{	
				if (scheduledMap.containsKey(i))
				{
				
					((TextView)textButton).setTextColor(Color.BLACK);
					
					switch (entity.mode)
					{
					case PRIVATE:
						textButton.setBackgroundColor(Color.RED);
						break;
					case PUBLIC:
						textButton.setBackgroundColor(Color.BLUE);
						break;
					case COUPLE:
						textButton.setBackgroundColor(Color.YELLOW);
						break;
					}
			
					
				}else if(reservedMap.containsKey(i))
				{
					textButton.setBackgroundColor(Color.LTGRAY);  //reserved
				
						switch (entity.mode)
						{
						case PRIVATE:
							((TextView)textButton).setTextColor(Color.RED);
							break;
						case PUBLIC:
							((TextView)textButton).setTextColor(Color.BLUE);
							break;
						case COUPLE:
							((TextView)textButton).setTextColor(Color.YELLOW);
							break;
						}
				
				}
	 
			 textButton.setText(ConstantProvider.getHour(entity.hour) + ":" +entity.workType+":"+entity.comment);
			 addView(textButton, params);
			
			 if (ActivityNumber == VIEWDAYDIARY)
			 {
			 	textButton.setOnClickListener(new OnClickListener(){

					public void onClick(View view) {
						// TODO Auto-generated method stub
					
						Intent intent = new Intent(context, EditDayDiary.class);
						Bundle bundle = new Bundle();
						bundle.putString("year", dateArray[0]);
						bundle.putString("month", dateArray[1]);
						bundle.putString("date", dateArray[2]);
						bundle.putString("time", ""+view.getId());
						bundle.putInt("mode", Integer.parseInt(dateArray[3]));
						intent.putExtras(bundle);
						((Activity) context).startActivityForResult(intent,0);
						
					
					}
					
				});

			}
			}	
		}	
		
	}
		
	public void setActionListener(int ActivityNumber)
	{
		this.ActivityNumber = ActivityNumber;
		
	}
	
} 
	 	
	

